package com.iwinding.ladygg.ui.act;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.response.PersonInfoRp;
import com.iwinding.ladygg.utils.GsonUtil;
import com.iwinding.ladygg.utils.SPUtils;

/**
 * Created by hezhiyong on 2017/3/20.
 */

public class QrcodeActivity extends BaseActivity {

    private SimpleDraweeView mImgPhoto;
    private SimpleDraweeView mImgQrcode;
    private TextView mTvName;
    private TextView mTvID;
    private TextView mTvPhone;

    public static final String LASTACTNAME = "last_act_name";

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_qrcode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    void initView() {
        String leftstr = getIntent().getStringExtra(LASTACTNAME);
        setTitleStr(leftstr);

        mImgPhoto = (SimpleDraweeView) findViewById(R.id.icane_my_img);
        mImgQrcode = (SimpleDraweeView) findViewById(R.id.icane_my_qrcode);
        mTvName = (TextView) findViewById(R.id.icane_my_txt_name);
        mTvID = (TextView) findViewById(R.id.icane_my_txt_id);
        mTvPhone = (TextView) findViewById(R.id.icane_my_txt_phone);

        String str = SPUtils.getSpPropertyString(mContext, SPConstant.PERSONINFO,"");
        if(!TextUtils.isEmpty(str)){
            PersonInfoRp personInfoRp = GsonUtil.getInstance().formJson(str,PersonInfoRp.class);
            setInfo(personInfoRp.headImg,personInfoRp.QRCodeImg,personInfoRp.name,personInfoRp.userId,personInfoRp.mobile);
        }else {
            finish();
        }
    }

    /**
     * 设置基础信息
     *
     * @param imageUrl
     * @param qrcodeUrl
     * @param name
     * @param id
     * @param phone
     */
    void setInfo(String imageUrl, String qrcodeUrl, String name, String id, String phone) {
        if (!TextUtils.isEmpty(imageUrl)) {
            try {
                Uri uri = Uri.parse(imageUrl);
                mImgPhoto.setImageURI(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!TextUtils.isEmpty(name)) {
            mTvName.setText(name);
        }
        if (!TextUtils.isEmpty(id)) {
            mTvID.setText(id);
        }
        if (!TextUtils.isEmpty(phone)) {
            mTvPhone.setText(phone);
        }
        if (!TextUtils.isEmpty(qrcodeUrl)) {
            try {
                Uri uri = Uri.parse(qrcodeUrl);
                mImgQrcode.setImageURI(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
